const Error = require('../config/error-code')
const { Encrypt } = require('../utils/secret')

module.exports = async (req,res,next) => {
    // 校验请求时间戳是否过期
    let timeStamp = req.headers.timestamp;
    let signTimeStamp = req.headers.signtimestamp;
    if(!timeStamp || !signTimeStamp) {
        return res.status(400).end();
    }
    let encryptTime = Encrypt(timeStamp)
    if(signTimeStamp !== encryptTime) {
        return res.status(400).end();
    }
    let currentTime = new Date().getTime()
    if(currentTime - timeStamp > 60000) {
        return res.status(200).json({
            success:false,
            errcode:Error['XP002'].errcode,
            errmsg:Error['XP002'].errmsg,
            data:{}
        });
    }
    next()
}